package com.allwees.bs.module.pms.rest;

import com.allwees.bs.core.model.constant.CacheName;
import com.allwees.bs.core.modelbase.vo.R;
import com.allwees.bs.module.core.annotation.PrintLog;
import com.allwees.bs.module.core.rest.BoCrudApi;
import com.allwees.bs.module.pms.dto.ProductDto;
import com.allwees.bs.module.pms.entity.Product;
import com.allwees.bs.module.pms.entity.Sku;
import com.allwees.bs.module.pms.req.PutoffProductReq;
import com.allwees.bs.module.pms.req.PutonProductReq;
import com.allwees.bs.module.pms.req.SetCategoryReq;
import com.allwees.bs.module.pms.service.BaseProductService;
import com.allwees.bs.module.pms.service.ProductOnoffLogService;
import com.allwees.bs.module.pms.service.SkuService;
import com.allwees.core.cache.RedisUtil;
import com.allwees.core.common.util.reflect.PojoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @param <S>
 * @author yangylsky
 * @since 2019-12-01
 */
@PrintLog
@Slf4j
@RestController
public abstract class BaseProductApi<S extends BaseProductService> extends BoCrudApi<Product, S> {
    @Autowired
    protected SkuService skuService;

    @Autowired
    protected ProductOnoffLogService productOnoffLogService;

    @Override
    protected Object _doRetrieve(String productUuid) {
        Product product = boService.findByUuid(productUuid);
        ProductDto productDto = ProductDto.of(product);
        if (product != null) {
            List<Sku> skus = skuService.getByProductUuid(productUuid);
            productDto.setSkus(PojoUtil.copy(new ArrayList<>(), skus));
        }
        return productDto;
    }

    /**
     * 上架
     *
     * @param req
     * @return
     */
    @PostMapping("/puton")
    public R puton(@RequestBody PutonProductReq req) {
        boService.puton(req);
        return R.ok("上架成功");
    }

    /**
     * 下架
     *
     * @param req
     * @return
     */
    @PostMapping("/putoff")
    public R putoff(@RequestBody PutoffProductReq req) {
        boService.putoff(req);
        return R.ok("下架成功");
    }

    /**
     * 批量修改类目
     * @param req
     * @return
     */
    @PutMapping("/category")
    public R batchUpdateCategory(@RequestBody SetCategoryReq req) {
        boService.batchUpdateCategory(req);
        return R.ok();
    }

    public void removeFromCache(List<Product> products){
        if(CollectionUtils.isEmpty(products)){
            return;
        }
        for (Product product : products) {
            RedisUtil.del(
                    CacheName.PROD_PRODUCT_BY_UUID + "::" + product.getUuid(),
                    CacheName.PROD_PRODUCT_DETAILS + "::" + product.getUuid()
            );
        }
    }
}
